package com.moinut.studentattendancemanager.view.teacher;

import com.moinut.studentattendancemanager.controller.StudentController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnGetListener;
import com.moinut.studentattendancemanager.model.vo.db.Student;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author MOILING
 */
public class GetStudentFrame extends BaseFrame<GetStudentFrame.GetStudentPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIGHT = 580;

    public GetStudentFrame() {
        super("查看学生信息", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected GetStudentPanel onSetContentPane() {
        return new GetStudentPanel();
    }

    /* package */ class GetStudentPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JPanel mContentPanel, mButtonPanel;
        private JPanel mOptionPanel, mSearchPanel, mResultPanel;
        private JScrollPane mResultScrollPane;
        private JTable mResultTable;
        private JLabel mSearchWayLabel, mKeywordLabel;
        private JComboBox<String> mSearchWayComboBox;
        private JTextField mKeywordTextField;
        private JButton mSearchButton, mRetryButton, mBackButton;
        private DefaultTableModel mTableModel;

        /*package*/ GetStudentPanel() {
            setLayout(new BorderLayout());
            initContentPanel();
            initButtonPanel();
        }

        private void initButtonPanel() {
            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mRetryButton = new JButton("重新开始查询");
            mBackButton = new JButton("返回上级界面");
            mButtonPanel.add(mRetryButton);
            mButtonPanel.add(mBackButton);
            add(mButtonPanel, BorderLayout.SOUTH);

            mBackButton.addActionListener(this);
            mRetryButton.addActionListener(this);
        }

        private void initContentPanel() {
            mContentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            initOptionPanel();
            initSearchPanel();
            initResultPanel();
            add(mContentPanel, BorderLayout.CENTER);
        }

        private void initResultPanel() {
            mResultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            String[] columnName = {"姓名", "学号", "所属学院", "专业"};
            mTableModel = new DefaultTableModel(null, columnName);
            mResultTable = new JTable(mTableModel);
            mResultScrollPane = new JScrollPane(mResultTable,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            mResultPanel.add(mResultScrollPane);
            mContentPanel.add(mResultPanel);
        }

        private void initSearchPanel() {
            mSearchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mKeywordLabel = new JLabel("请输入查询关键字");
            mKeywordTextField = new JTextField(20);
            mSearchButton = new JButton("查询");
            mSearchPanel.add(mKeywordLabel);
            mSearchPanel.add(mKeywordTextField);
            mSearchPanel.add(mSearchButton);
            mContentPanel.add(mSearchPanel);
            mSearchButton.addActionListener(this);
        }

        private void initOptionPanel() {
            mOptionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mSearchWayLabel = new JLabel("请选择查询方式     ");
            mSearchWayComboBox = new JComboBox<>();
            mSearchWayComboBox.addItem("按姓名查询");
            mSearchWayComboBox.addItem("按教学班查询");
            mSearchWayComboBox.addItem("按专业查询");
            mSearchWayComboBox.addItem("按学号查询");

            mOptionPanel.add(mSearchWayLabel);
            mOptionPanel.add(mSearchWayComboBox);
            mContentPanel.add(mOptionPanel);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(GetStudentFrame.this);
            } else if (e.getSource().equals(mRetryButton)) {
                mKeywordTextField.setText("");
                clearTable();
            } else if (e.getSource().equals(mSearchButton)) {
                switch (mSearchWayComboBox.getSelectedIndex()) {
                    case 0:
                        StudentController.getStudentsByName(mKeywordTextField.getText(), new StudentSearchListener());
                        break;
                    case 1:
                        StudentController.getStudentsByClass(mKeywordTextField.getText(), new StudentSearchListener());
                        break;
                    case 2:
                        StudentController.getStudentsByMajor(mKeywordTextField.getText(), new StudentSearchListener());
                        break;
                    case 3:
                        StudentController.getStudentById(mKeywordTextField.getText(), new OnGetListener<Student>() {
                            @Override
                            public void onSuccess(Student student) {
                                clearTable();
                                mTableModel.addRow(new Object[]{
                                        student.getStudentName(),
                                        student.getStudentId(),
                                        student.getStudentCollege(),
                                        student.getStudentMajor()
                                });
                            }

                            @Override
                            public void onError(String error) {
                                clearTable();
                                JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                        break;
                }
            }
        }

        private void clearTable() {
            for (int i = mTableModel.getRowCount() - 1; i >= 0; i--) {
                mTableModel.removeRow(i);
            }
        }

        class StudentSearchListener implements OnGetListListener<Student> {
            @Override
            public void onSuccess(List<Student> students) {
                clearTable();
                for (Student student : students) {
                    mTableModel.addRow(new Object[]{
                            student.getStudentName(),
                            student.getStudentId(),
                            student.getStudentCollege(),
                            student.getStudentMajor()
                    });
                }
            }

            @Override
            public void onError(String error) {
                clearTable();
                JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
